import axios from 'axios';
import { Message } from 'element-ui';
import JsCookie from '@/utils/cookies'
const Cookies = new JsCookie('TENANT_ID')

const instance = axios.create({
    // timeout: 10000,
    headers: {
        'Access-Control-Allow-Origin': '*',
        'content-type': 'application/json;charset=UTF-8',
    }
})

instance.interceptors.request.use(
    config => {
        // console.log('拦截成功', config.headers);
        const token = localStorage.getItem('token')
        const tenantId = Cookies.get() || localStorage.getItem('tenantId')
        if (token) {
            config.headers['Authorization'] = `Bearer ${token}`;
            config.headers['TENANT-ID'] = tenantId;
        }
        return config;
    }, 
    error => {
        // console.log('拦截失败', error);
        return Promise.reject(error);
    }
);

instance.interceptors.response.use(
    response => {
        // console.log('响应成功', response);
        const res = response.data
        return res
    }, 
    error => {
        // console.log('响应失败', error);
        Message.error('接口错误请联系管理员')
        return Promise.reject(error);
    }
);

export default instance